package com.zcsy.manage.web.base;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.ws.rs.core.Context;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zcsy.commons.constants.Constants;
import com.zcsy.commons.model.RequestConstants;
import com.zcsy.commons.model.RequestJson;
import com.zcsy.commons.util.DateUtils;
import com.zcsy.manage.model.base.CellInfo;
import com.zcsy.manage.model.base.CommonConfig;
import com.zcsy.manage.model.system.Dict;
import com.zcsy.manage.model.system.User;
import com.zcsy.manage.service.base.ICellInfoService;
import com.zcsy.manage.service.base.ICommonConfigService;
import com.zcsy.manage.service.system.IDictService;
import com.zcsy.manage.web.common.LogUtils;


/**
 * @Title: CommonConfigController.java
 * @Package： com.zcsy.manage.web.property
 * @Description: 小区配置控制层管理类
 * @author xujl
 * @Copyright: 2015 武汉中财数元信息技术有限公司 All rights reserved.
 * @date 2016年4月14日
 * @version 1.0
 * @see JDK1.7.0_75
 * @since
 */
@Controller
@RequestMapping(value = "/commonConfig")
public class CommonConfigController {

    @Autowired
    private ICommonConfigService configService;

    @Autowired
    private ICellInfoService cellInfoService;

	@Autowired
	private IDictService dictService;
	
    private final static int PLATFORMTYPE = 1;//平台配置
    private final static int CELLTYPE     = 2;//小区配置
    private final static int PROPERTYTYPE = 3;//物业配置

    /**
     * @Description 获取所有的配置信息数据列表
     * @param model
     * @param req
     *            用户的请求
     * @return 返回的结果配置信息列表
     * @author xujl
     * @date 2016年4月25日
     * @see
     */
    @RequestMapping(value = "infoList", method = RequestMethod.GET)
    public String infoList(Model model,Integer pNo, Integer pSize, String label, String cellId, HttpServletRequest req) {

        if (pNo == null) {
            pNo = Constants.PAGE_NO;
        }

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
        // 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, pageSize);
        CommonConfig record = new CommonConfig();
        
        if (StringUtils.isNotEmpty(cellId)) {
            record.setCellId(cellId);
        }
        if (StringUtils.isNotEmpty(label)) {
            record.setLabel(label);
        }
        
        List<CommonConfig> infoList = configService.selectByKeyword(record);
        PageInfo<CommonConfig> info = new PageInfo<CommonConfig>(infoList);
        List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();

        model.addAttribute("infoList", info);
        model.addAttribute("cellInfos", cellInfos);
        model.addAttribute("label", label);
        model.addAttribute("cellId", cellId);
        model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

        return "base/commonconfig/configList";
    }

    /**
     * @Description 根据id得到详情<br>
     * @param id
     *            主键id
     * @return 跳转画面URL
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "gotoDetail", method = RequestMethod.GET)
    public String gotoDetail(Model model, String id) {

        String url = "";
        try {

            CommonConfig record = configService.selectByPrimaryKey(id);

            model.addAttribute("config", record);
            url = "base/commonconfig/configDetail";

        } catch (Exception e) {
        }
        return url;
    }
    /**
     * @Description 根据id得到详情<br>
     * @param id
     *            主键id
     * @return 跳转画面URL
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "gotoDetailForCell", method = RequestMethod.GET)
    public String gotoDetailForCell(Model model, String id) {

        String url = "";
        try {

            CommonConfig record = configService.selectByPrimaryKey(id);

            model.addAttribute("config", record);
            url = "base/commonconfig/configDetailForCell";

        } catch (Exception e) {
        }
        return url;
    }
    /**
     * @Description 根据id得到详情<br>
     * @param id
     *            主键id
     * @return 跳转画面URL
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "gotoDetailForProperty", method = RequestMethod.GET)
    public String gotoDetailForProperty(Model model, String id) {

        String url = "";
        try {

            CommonConfig record = configService.selectByPrimaryKey(id);

            model.addAttribute("config", record);
            url = "base/commonconfig/configDetailForProperty";

        } catch (Exception e) {
        }
        return url;
    }

    /**
     * @Description 根据id得到详情<br>
     * @param id
     *            主键id
     * @return 跳转画面URL
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "editConfig", method = RequestMethod.GET)
    public String editConfig(Model model, String id, Integer editType) {

        String url = "";
        try {

            CommonConfig record = configService.selectByPrimaryKey(id);
            List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();

            model.addAttribute("editType", editType);
            model.addAttribute("config", record);
            model.addAttribute("cellInfos", cellInfos);
            url = "base/commonconfig/configEdit";

        } catch (Exception e) {
        }
        return url;
    }
   
    /**
     * @description 跳转小区配置的编辑页面
     * @param id 主键id
     * @param editType 区分值的输入是图文混排还是文本类型
     * @return
     * String 小区配置页面路径
     * @author chenxin
     * @date 2016年9月20日上午11:34:33
     */
    @RequestMapping(value = "editConfigForCell", method = RequestMethod.GET)
    public String editConfigForCell(Model model, String id, Integer editType) {

        String url = "";
        try {

            CommonConfig record = configService.selectByPrimaryKey(id);
            List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();

            if(StringUtils.equals(record.getName(), "cell_service_phone1") || StringUtils.equals(record.getName(), "cell_service_phone2")) {
                editType = Constants.cell_service_phone_TYPE;
            }
            model.addAttribute("editType", editType);
            model.addAttribute("config", record);
            model.addAttribute("cellInfos", cellInfos);
            url = "base/commonconfig/configEditForCell";

        } catch (Exception e) {
        }
        return url;
    }
    /**
     * @Description 根据id得到详情<br>
     * @param id
     *            主键id
     * @return 跳转画面URL
     * @author xujl
     * @date 2016年4月14日
     * @see
     */
    @RequestMapping(value = "editConfigForProperty", method = RequestMethod.GET)
    public String editConfigForProperty(Model model, String id,Integer editType) {

        String url = "";
        try {

            CommonConfig record = configService.selectByPrimaryKey(id);
            if(StringUtils.equals(record.getName(), "property_feebill_paytime")) {
                editType = Constants.PROPERTY_FEEBILL_PAYTIME_TYPE;
            }
            List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();

            model.addAttribute("editType", editType);
            model.addAttribute("config", record);
            model.addAttribute("cellInfos", cellInfos);
            url = "base/commonconfig/configEditForProperty";

        } catch (Exception e) {
        }
        return url;
    }

    /**
     * @Description 编辑配置对象
     * @param id id
     * @param label 描述
     * @param cellId
     *            小区
     * @param unit
     *            单位
     * @param value
     *            值
     * @param memo
     *            备注
     * @param req
     *            用户的请求
     * @return 保存的结果
     * @author xujl
     * @date 2016年5月11日
     * @see
     */
    @RequestMapping(value = "saveConfig", method = RequestMethod.POST)
    @ResponseBody
    public RequestJson saveConfig(String id, String label, String cellId, String unit,
                                  String value, String memo, @Context HttpServletRequest req) {
        RequestJson result = new RequestJson();
        final HttpSession session = req.getSession();
        User user = (User)session.getAttribute(RequestConstants.USER_SESSION_ID);

        CommonConfig record = new CommonConfig();
        record.setLabel(label);
        record.setCellId(cellId);
        record.setUnit(unit);
        record.setValue(value);
        record.setId(id);
        record.setMemo(memo);
        record.setUpdatePerson(user.getUsername());
        record.setUpdateTime(DateUtils.getDate());

        try {
            configService.update(record);
            result.setSuccess(true);
            LogUtils.insertLog("用户编辑配置信息", user.getId(), req);
        } catch (Exception e) {
            result.setMsg("操作异常");
            result.setSuccess(false);
        }
        return result;
    }
    
    /**
     * @description 平台的配置列表信息
     * @author chenxin
     * @date 2016年8月23日下午5:27:29
     */
    @RequestMapping(value = "infoListForPlatform", method = RequestMethod.GET)
    public String infoListForPlatform(Model model,Integer pNo, Integer pSize, String label,String cellId, HttpServletRequest req) {

        if (pNo == null) {
            pNo = Constants.PAGE_NO;
        }

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		
        CommonConfig record = new CommonConfig();
        
        if (StringUtils.isNotEmpty(cellId)) {
            record.setCellId(cellId);
        }
        if (StringUtils.isNotEmpty(label)) {
            record.setLabel(label);
        }
        record.setConfigType(PLATFORMTYPE);
       
        // 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, pageSize);
        List<CommonConfig> infoList = configService.selectByKeyword(record);
        PageInfo<CommonConfig> info = new PageInfo<CommonConfig>(infoList);
        List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();
        
        model.addAttribute("infoList", info);
        model.addAttribute("cellInfos", cellInfos);
        model.addAttribute("label", label);
        model.addAttribute("cellId", cellId);
        model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

        return "base/commonconfig/configList";
    }
    
    /**
     * @description 小区的配置列表信息
     * @author chenxin
     * @date 2016年8月23日下午5:27:29
     */
    @RequestMapping(value = "infoListForCell", method = RequestMethod.GET)
    public String infoListForCell(Model model,Integer pNo, Integer pSize, String label, String cellId, HttpServletRequest req) {

        if (pNo == null) {
            pNo = Constants.PAGE_NO;
        }

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String cellID = user.getCurrentCell().getId();
       
        CommonConfig record = new CommonConfig();
        
        if (StringUtils.isNotEmpty(cellId)) {
            record.setCellId(cellId);
        }
        if (StringUtils.isNotEmpty(label)) {
            record.setLabel(label);
        }
        record.setConfigType(CELLTYPE);
        record.setCellId(cellID);
        
        // 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, pageSize);
        List<CommonConfig> infoList = configService.selectByKeyword(record);
        PageInfo<CommonConfig> info = new PageInfo<CommonConfig>(infoList);
        
        List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();
        
        model.addAttribute("infoList", info);
        model.addAttribute("cellInfos", cellInfos);
        model.addAttribute("label", label);
        model.addAttribute("cellId", cellId);
        model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

        return "base/commonconfig/configListForCell";
    }
    
    /**
     * @description 物业的配置列表信息
     * @author chenxin
     * @date 2016年8月23日下午5:27:29
     */
    @RequestMapping(value = "infoListForProperty", method = RequestMethod.GET)
    public String infoListForProperty(Model model,Integer pNo, Integer pSize, String label, String cellId, HttpServletRequest req) {

        if (pNo == null) {
            pNo = Constants.PAGE_NO;
        }

		Integer pageSize;
		if (pSize == null) {
			pageSize = Constants.PAGE_SIZE;
		} else {
			switch (pSize) {
				case 0: pageSize = Constants.PAGE_SIZE;
				break;
				case 1: pageSize = Constants.PAGE_SIZE_TWENTY;
				break;
				case 2: pageSize = Constants.PAGE_SIZE_FIFTY;
				break;
				default:pageSize = Constants.PAGE_SIZE;
				break;
			}
		}
		
		User user = (User)req.getSession().getAttribute(RequestConstants.USER_SESSION_ID);
        String cellID = user.getCurrentCell().getId();
        
        CommonConfig record = new CommonConfig();
        
        if (StringUtils.isNotEmpty(cellId)) {
            record.setCellId(cellId);
        }
        if (StringUtils.isNotEmpty(label)) {
            record.setLabel(label);
        }
        record.setConfigType(PROPERTYTYPE);
        record.setCellId(cellID);
        
        // 此处添加PageHelper将会走拦截器，进行分页，分页方法只针对紧跟着PageHelper的查询方法有效
        PageHelper.startPage(pNo, pageSize);
        List<CommonConfig> infoList = configService.selectByKeyword(record);
        PageInfo<CommonConfig> info = new PageInfo<CommonConfig>(infoList);
        
        List<CellInfo> cellInfos = cellInfoService.getAllCellInfo();
        
        model.addAttribute("infoList", info);
        model.addAttribute("cellInfos", cellInfos);
        model.addAttribute("label", label);
        model.addAttribute("cellId", cellId);
        model.addAttribute("currentPage", pNo);
		model.addAttribute("pSize", pSize);

		List<Dict> pageSizes = dictService.getDictFromCache("pageSize");
		model.addAttribute("pageSizes", pageSizes);

        return "base/commonconfig/configListForProperty";
    }

}
